package com.zzb.weikeautosign;

import com.zzb.weikeautosign.param.LoginParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by 瑱彬 on 2016/1/15.
 */
public class SecurityUtils {
    public static String getHttpParamsSecuryKey(Map<String, String> paramMap) {
        TreeMap<String,String> localTreeMap = new TreeMap<>(paramMap);
        StringBuilder localStringBuilder = new StringBuilder();
        localStringBuilder.append("ADowdfmKFKwla021fkWcmPCUY");
        String paramKey = null;
        String paramValue = null;
        for (Map.Entry<String, String> stringEntry : localTreeMap.entrySet()) {
            try {
                paramKey = stringEntry.getKey();
                if (!"sign".equals(paramKey)) {
                    paramValue = stringEntry.getValue();
                    if (StringUtils.isNotBlank(stringEntry.getValue())) {
                        localStringBuilder.append(paramKey).append("=").append(URLEncoder.encode(paramValue, "utf-8")).append("$");
                    }
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        if (!localTreeMap.isEmpty())
            localStringBuilder.deleteCharAt(localStringBuilder.length() - 1);
        localStringBuilder.append("ADowdfmKFKwla021fkWcmPCUY");
        return DigestUtils.md5Hex(localStringBuilder.toString());
    }

}
